<?php
/**
 * Created by PhpStorm.
 * User: LZJ
 * Date: 2014/11/15
 * Time: 21:25
 */

require "Curl.php";

class URPCurl
{
    private $loginUrl;
    private $targetUrl;
    private $curl;
    private $simulateParams;
    private $params;
    private $cookies;

    function __construct($loginUrl, array $simulateParams = null,
                         $targetUrl = null, array $params = null)
    {
        $this->loginUrl = $loginUrl;
        $this->targetUrl = $targetUrl;
        $this->setSimulateParams($simulateParams);
        $this->setParams($params);
        $this->curl = new Curl($loginUrl);
    }

    function simulate()
    {
        $curl = $this->curl;
        $curl->dev();
        $curl->setParams($this->simulateParams);
        $curl->exec();

    }

    function setSimulateParams(array $params)
    {
        if ($params == null || count($params) == 0)
            return;
        $this->simulateParams = $params;
    }

    function setParams(array $params = null)
    {
        if ($params == null || count($params) == 0)
            return;
        $this->params = $params;
    }

    public function exec()
    {
        if ( $this->targetUrl != null)
        {
            $this->simulate();
            $curl = $this->curl;
            $curl->setCookies($curl->getCookies());
            $curl->setUrl($this->targetUrl);
            $curl->dev();
            return $curl->exec();
        }
        else
        {
            throw new Exception("please set your targetUrl before you exec");
        }
    }

}


?>